import Vue from 'vue'
import VueRouter from 'vue-router'
import AdminIndex from '@/views/AdminIndex'
import AdminHome from '@/views/AdminHome'
import AdminLogin from '@/views/AdminLogin'
import AuthAdd from '@/views/auth/AuthAdd'
import AuthList from '@/views/auth/AuthList'
import AuthEdit from '@/views/auth/AuthEdit'
import UserAdd from '@/views/user/UserAdd'
import UserList from '@/views/user/UserList'
import UserEdit from '@/views/user/UserEdit'
import RoleAdd from '@/views/role/RoleAdd'
import RoleList from '@/views/role/RoleList'
import RoleEdit from '@/views/role/RoleEdit'
import store from '@/store'
import {getCookie} from '@/utils/cookie'
import NotFound from '@/components/NotFound'
import CatList from '@/views/cat/CatList'
import CatAdd from '@/views/cat/CatAdd'
import DishAdd from '@/views/dish/DishAdd'
import DishList from '@/views/dish/DishList'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: AdminIndex,
    redirect: '/home'
  },
  {
    path: '/home',
    component: AdminHome,
    children: [
      {
        path: '/auth/add',
        component: AuthAdd
      },
      {
        path: '/auth/list',
        component: AuthList
      },
      {
        path: '/auth/edit',
        component: AuthEdit
      },
      {
        path: '/user/add',
        component: UserAdd
      },
      {
        path: '/user/list',
        component: UserList
      },
      {
        path: '/user/edit',
        component: UserEdit
      },
      {
        path: '/role/add',
        component: RoleAdd
      },
      {
        path: '/role/list',
        component: RoleList
      },
      {
        path: '/role/edit',
        component: RoleEdit
      },
      {
        path: '/cat/list',
        component: CatList
      },
      {
        path: '/cat/add',
        component: CatAdd
      },
      {
        path: '/dish/list',
        component: DishList
      },
      {
        path: '/dish/add',
        component: DishAdd
      },
    ]
  },
  {
    path: '/login',
    component: AdminLogin
  },
  {
    path: '/404',
    component: NotFound
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(1);
  if(to.path === '/login') {
    next()
    return
  }
  // console.log(2);
  let username = getCookie('adminname')
  username && store.commit('changeLoginStatus')
  // console.log(username, store.state.isLogin);
  if(!username || store.state.isLogin === false) {
    router.push('/login')
    return
  }
  // console.log(3);
  if(to.path === '/404') {
    next()
    return
  }
  // console.log(4);
  // 将用户具有的所有权限对应的路径组成数组
  let authsArr = []
  // console.log(777, store.state.auths);
  store.state.auths.forEach(v => {
    // console.log(v);
    if(v.path != '-') {
      // console.log(666, v.path);
      authsArr.push(v.path)
    }
    v.children.forEach(item => authsArr.push(item.path))
  })
  // 判断路径是否在用户拥有的权限内
  let index = authsArr.indexOf(to.path)
  // console.log(authsArr, to.path);
  if(index < 0) {
    router.push('/404')
    return
  }
  // console.log(5);
  next()
})

export default router
